package Controller;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import Model.Data;
import Model.TokenManager;

/* Recieve all the messages and handle them */ 
public class Server 
{
	// which port the server listens to
	private final int INCOMING_PORT = 1234; 
	
	public void start() 
	{	
		// init data
		Data.getInstance();
		TokenManager.getInstance();
		
		ServerSocket serverSocket = null;
		Socket connSocket = null;
		
		try 
		{
	         
			// start listening
			serverSocket = new ServerSocket(INCOMING_PORT);
			
			while (true)
			{
				// accept connections
				connSocket = serverSocket.accept();
				 
				// create separate thread for each request
				Request request = new Request(connSocket);
				request.start();
			}
		}
        catch(Exception e) 
        {

        }
		finally
		{
			try 
			{
				connSocket.close();
			} 
			catch (IOException e) 
			{

			}
			
			try 
			{
				serverSocket.close();
			} 
			catch (IOException e) 
			{

			}			
		}
	}
}
